using B.Z.BasicData.Infrastructure;
using B.Z.Inventory.API.Read.Application.Command;
using B.Z.Inventory.API.Read.DTO;
using B.Z.Inventory.Domain;
using B.Z.Inventory.ErrorCode;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.Z.Inventory.API.Read.Application.CommandHandler
{
    /// <summary>
    /// 库存盘点任务数据获取命令处理器
    /// </summary>
    public class InventoryDataCommandHandler : IRequestHandler<InventoryDataCommand, APIResult<DataInventoryDto>>
    {
        private readonly IBaseRepository<T_InventoryTask> inventoryTaskRepository;
        private readonly IBaseRepository<T_Store> storeRepository;
        private readonly ILogger<InventoryDataCommandHandler> logger;

        public InventoryDataCommandHandler(IBaseRepository<T_InventoryTask> inventoryTaskRepository, ILogger<InventoryDataCommandHandler> logger, IBaseRepository<T_Store> storeRepository)
        {
            this.inventoryTaskRepository = inventoryTaskRepository;
            this.logger = logger;
            this.storeRepository = storeRepository;
        }

        public async Task<APIResult<DataInventoryDto>> Handle(InventoryDataCommand request, CancellationToken cancellationToken)
        {
            APIResult<DataInventoryDto> result = new APIResult<DataInventoryDto>();
            result.Code = APIEnums.ok;

            try
            {
                // 根据ID获取库存盘点任务实体
                var inventoryTask = (from a in inventoryTaskRepository.GetAll().AsNoTracking()
                                    join b in storeRepository.GetAll().AsNoTracking()
                                    on a.StoreId equals b.Id
                                    select new DataInventoryDto 
                                    {
                                        Id = a.Id,
                                        InventoryNnumber = a.InventoryNnumber,
                                        InventoryStatus = a.InventoryStatus,
                                        Mid = a.Mid,
                                        InventoryType = a.InventoryType,
                                        InventoryRange = a.InventoryRange,
                                        InventoryStartTime = a.InventoryStartTime,
                                        InventoryEndTime = a.InventoryEndTime,
                                        StoreId=a.StoreId,
                                        Note = a.Note,
                                        TaskType = a.TaskType,
                                        AttnId = a.AttnId,
                                        AttnName = a.AttnName,
                                        CreateUserName = a.CreateUserName,

                                        StoreName = b.StoreName,
                                        IsHaveGoods = a.IsHaveGoods,
                                        CreateTime= a.CreateTime
                                    }).FirstOrDefault(x=>x.Id==request.Id);

                if (inventoryTask == null)
                {
                    result.Code = APIEnums.fail;
                    result.Message = "未找到指定的库存盘点任务";
                    logger.LogWarning($"未找到ID为{request.Id}的库存盘点任务");
                    return result;
                }

                result.Data = inventoryTask;
                result.Message = "获取库存盘点任务成功";
                
                logger.LogInformation($"成功获取ID为{request.Id}的库存盘点任务");
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"获取ID为{request.Id}的库存盘点任务时发生错误");
                result.Code = APIEnums.fail;
                result.Message = "获取库存盘点任务失败";
            }

            return result;
        }
    }
}